import 'package:architecture/os_size.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../widgets/text_button.dart';
import 'auto_update_controller.dart';

class AutoUpdatePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<AutoUpdateController>(
        init: AutoUpdateController(),
        builder: (controller) {
          return Scaffold(
            appBar: AppBar(title: Text('自动更新')),
            body: Container(
              width: windowWidth * 0.9,
              height: 50,
              margin: EdgeInsets.only(
                  top: windowHeight * 0.01,
                  left: windowWidth * 0.01,
                  right: windowWidth * 0.01),
              child: Row(
                children: [
                  MyTextButton(
                    text: "检测更新",
                    confirm: () {
                      controller.checkUpdate();
                    },
                  ),
                  SizedBox(
                    width: 10,
                  ),
                  MyTextButton(
                    text: "下载",
                    confirm: () {
                      controller.download();
                    },
                  ),
                  SizedBox(
                    width: 10,
                  ),
                  MyTextButton(
                    text: "停止下载",
                    confirm: () {
                      controller.downloader.cancel();
                    },
                  ),
                  SizedBox(
                    width: 10,
                  ),
                  MyTextButton(
                    text: "安装",
                    confirm: () {
                      controller.installUpdate(controller.path);
                    },
                  ),
                  SizedBox(
                    width: 10,
                  ),
                  Obx(() => Container(
                      width: windowWidth * 0.6,
                      child: Row(
                        children: [
                          Container(
                              width: windowWidth * 0.5,
                              child: ClipRRect(
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(12)),
                                  child: LinearProgressIndicator(
                                    minHeight: 12,
                                    valueColor:
                                        AlwaysStoppedAnimation(Colors.green),
                                    backgroundColor: Colors.grey,
                                    value: controller.progress.value,
                                  ))),
                          SizedBox(
                            width: 5,
                          ),
                          Text(
                            (controller.progress.value * 100)
                                    .toStringAsFixed(2) +
                                '%',
                            style: TextStyle(color: Colors.green),
                          )
                        ],
                      ))),
                ],
              ),
            ),
          );
        });
  }
}
